/**
 * 
 */
package com.flashdeck.activity;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.math.RandomUtils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ViewFlipper;

import com.flashdeck.R;
import com.flashdeck.utils.DecksTags;

/**
 * @author Vincentas
 *
 */
public class DownloadDecksActivity extends Activity {
	
	private Set<String> selectedTags = new HashSet<String>();
	
	private ListView tagsListView;
	
	private ListView decksListView;
	
	private LinearLayout selectedTagsLinearLayout;
	
	private Button decksCountButton;
	
	private Button tagsCountButton;
	
	private DecksTags decksTags;
	
	private ViewFlipper viewFlipper;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Set<String> tags = new HashSet<String>(Arrays.asList(
				new String[] {
						"a", 
						"b", 
						"c", 
						"d", 
						"e", 
						"f", 
						"g", 
						"h", 
						"i", 
						"j", 
						"k", 
						"l", 
						"m", 
						"n", 
						"o", 
						"p", 
						"q", 
						"r", 
						"s", 
						"t", 
						"u", 
						"v", 
						"w",
						}));
		decksTags = getSampleDecksTags(500, tags);
		
		setContentView(R.layout.select_tags_layout);

		viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
		
		selectedTagsLinearLayout = (LinearLayout) findViewById(R.id.SelectedTagsLinearLayout);
		
		tagsCountButton = (Button) findViewById(R.id.tagsCountButton);
		tagsCountButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				viewFlipper.showNext();				
			}
		});
		
		decksCountButton = (Button) findViewById(R.id.DecksCountButton);
		decksCountButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				viewFlipper.showNext();
			}
		});
		
		decksListView = (ListView) findViewById(R.id.decksListView);
		decksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
				// TODO Auto-generated method stub
				
			}
		});
		
		tagsListView = (ListView) findViewById(R.id.TagsListView);
		tagsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
				ListAdapter listAdapter = (ListAdapter) arg0.getAdapter();
				String tag = (String) listAdapter.getItem(position);
				selectedTags.add(tag);
				updateTagsList();
				
				Button tagButton = new Button(DownloadDecksActivity.this);
				tagButton.setText(tag);
				selectedTagsLinearLayout.addView(tagButton);
				tagButton.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						selectedTagsLinearLayout.removeView(v);
						selectedTags.remove(((Button)v).getText());
						updateTagsList();
					}
				});
			}
		});
		updateTagsList();
	}
	
	private void updateTagsList() {
		tagsListView.setAdapter(new ArrayAdapter<String>(DownloadDecksActivity.this, android.R.layout.simple_list_item_1, decksTags.getAvailableTaqs(selectedTags).toArray(new String[0])));
		decksCountButton.setText(String.valueOf(decksTags.getDecks(selectedTags).size()));
	}

	private static DecksTags getSampleDecksTags(int decks, Set<String> tags) {
		DecksTags result = new DecksTags();
		
		for (int i = 0; i < decks; i++) {
			result.addDeck(Integer.toString(i), getRandomTags(RandomUtils.nextInt(tags.size() / 3), tags));
		}
		
		return result;
	}
	
	private static Set<String> getRandomTags(int count, Set<String> tags) {
		Set<String> result = new HashSet<String>(count);
		String[] tagsArray = tags.toArray(new String[0]);
		
		for (int i = 0; i < count; i ++) {
			result.add(tagsArray[RandomUtils.nextInt(tags.size())]);
		}
		
		return result;
	}

}
